<?php

class StudentQuery_BirthdayController extends Zend_Controller_Action
{

    public function init()
    {
        /* Initialize action controller here */
    }

    public function indexAction()
    {
        $year = $this->view->Semester['year'];
        $semester = $this->view->Semester['semester'];
        $q = Doctrine_Query::create()
        ->select('a.sn,MONTH(a.birthday) AS mm, COUNT(*) AS cc, b.grade AS grade , b.class_sn AS class_sn')
        ->from('Student_Model_Student a')
        ->leftJoin('a.Student_Model_StudentSemester b')
        ->whereIN('a.study_condition',
        array(Student_Model_StudentTransfer::IN_STADY,
        Student_Model_StudentTransfer::HOMESCHOOLING))
        ->andWhere('b.year=?', $year)
        ->andWhere('b.semester=?', $semester)
        ->groupBy('mm, grade, class_sn');
        $arr = array();
        $classArr = array();
        $monthArr = array();
        foreach($q->fetchArray() as $val) {
            if (! isset($classArr[$val['grade'].'-'.$val['class_sn']]))
            $classArr[$val['grade'].'-'.$val['class_sn']] = 0;

            if (! isset($monthArr[$val['mm']]))
            $monthArr[$val['mm']] = 0;

            $arr[$val['grade'].'-'.$val['class_sn']][$val['mm']] = $val['cc'];
            $classArr[$val['grade'].'-'.$val['class_sn']] += $val['cc'];
            $monthArr[$val['mm']] += $val['cc'];
        }

        $this->view->data = $arr;
        $this->view->classArr = $classArr;
        $this->view->monthArr = $monthArr;

          //班級陣列
        $semesterClass = new Semester_Model_SemesterClass();
        $this->view->className = $semesterClass->getClassNames($year,$semester);

        // 教職員部份
        $q = Doctrine_Query::create()
        ->select('sn, MONTH(birthday) AS mm, COUNT(*) AS cc')
        ->from('Teacher_Model_Teacher')
        ->where('condition_state=?', 0)
        ->groupBy('mm');
        $teacherArr = array();
        foreach($q->fetchArray() as $val)
        $teacherArr[$val['mm']] = $val['cc'];
        $this->view->teacherCount = $teacherArr;
    }


}

